package tankgame;

import java.util.Map;
import java.util.Vector;

/**
 * @author: lizhiwen
 * @version: 1.0
 */
public class EnemyTank extends Tank implements Runnable{
    Vector<Shot> shots = new Vector<>();
    Vector<EnemyTank> enemyTanks = new Vector<>();
    Vector<Wall> walls = new Vector<>();

    public EnemyTank(int x, int y) {
        super(x, y);
    }

    public void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        this.enemyTanks = enemyTanks;
    }

    public void setWalls(Vector<Wall> walls) {
        this.walls = walls;
    }

    //判断该坦克是否和其他坦克重叠
    public boolean isOverlap(){

        switch (this.getDirect()){
            case 0 : //上
                for (int i = 0;i < enemyTanks.size(); i++){
                    EnemyTank enemyTank = enemyTanks.get(i);
                    if (enemyTank != this){
                        //当敌方坦克方向为上/下时
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2){
                            if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 40
                            && this.getY() >= enemyTank.getY() &&this.getY() <= enemyTank.getY() +60){
                                return true;
                            }
                            if (this.getX() + 40 >= enemyTank.getX() && this.getX() + 40 <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY() &&this.getY() <= enemyTank.getY() +60){
                                return true;
                            }
                        }

                        //左右
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3){
                            if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY() &&this.getY() <= enemyTank.getY() +40){
                                return true;
                            }
                            if (this.getX() + 40 >= enemyTank.getX() && this.getX() + 40 <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY() &&this.getY() <= enemyTank.getY() +40){
                                return true;
                            }
                        }
                    }
                }
                break;
            case 1 : //右
                for (int i = 0;i < enemyTanks.size(); i++){
                    EnemyTank enemyTank = enemyTanks.get(i);
                    if (enemyTank != this){
                        //当敌方坦克方向为上/下时
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2){
                            if (this.getX() + 60 >= enemyTank.getX() && this.getX() + 60 <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY() &&this.getY() <= enemyTank.getY() +60){
                                return true;
                            }
                            if (this.getX() + 60 >= enemyTank.getX() && this.getX() + 60 <= enemyTank.getX() + 40
                                    && this.getY() + 40 >= enemyTank.getY() &&this.getY() + 40 <= enemyTank.getY() +60){
                                return true;
                            }
                        }

                        //左右
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3){
                            if (this.getX() + 60 >= enemyTank.getX() && this.getX() + 60 <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY() &&this.getY() <= enemyTank.getY() +40){
                                return true;
                            }
                            if (this.getX() + 60 >= enemyTank.getX() && this.getX() + 60 <= enemyTank.getX() + 60
                                    && this.getY() + 40 >= enemyTank.getY() &&this.getY() + 40 <= enemyTank.getY() +40){
                                return true;
                            }
                        }
                    }
                }
                break;
            case 2 : //下
                for (int i = 0;i < enemyTanks.size(); i++){
                    EnemyTank enemyTank = enemyTanks.get(i);
                    if (enemyTank != this){
                        //当敌方坦克方向为上/下时
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2){
                            if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 40
                                    && this.getY()  + 60 >= enemyTank.getY() &&this.getY()  + 60 <= enemyTank.getY() +60){
                                return true;
                            }
                            if (this.getX() + 40 >= enemyTank.getX() && this.getX() + 40 <= enemyTank.getX() + 40
                                    && this.getY() + 60 >= enemyTank.getY() &&this.getY() + 60 <= enemyTank.getY() +60){
                                return true;
                            }
                        }

                        //左右
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3){
                            if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 60
                                    && this.getY()  + 60 >= enemyTank.getY() &&this.getY()  + 60 <= enemyTank.getY() +40){
                                return true;
                            }
                            if (this.getX() + 40 >= enemyTank.getX() && this.getX() + 40 <= enemyTank.getX() + 60
                                    && this.getY() + 60 >= enemyTank.getY() &&this.getY() + 60 <= enemyTank.getY() +40){
                                return true;
                            }
                        }
                    }
                }
                break;
            case 3 : //左
                for (int i = 0;i < enemyTanks.size(); i++){
                    EnemyTank enemyTank = enemyTanks.get(i);
                    if (enemyTank != this){
                        //当敌方坦克方向为上/下时
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2){
                            if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY() &&this.getY() <= enemyTank.getY() +60){
                                return true;
                            }
                            if (this.getX() >= enemyTank.getX() && this.getX()  <= enemyTank.getX() + 40
                                    && this.getY() + 40  >= enemyTank.getY() &&this.getY() + 40  <= enemyTank.getY() +60){
                                return true;
                            }
                        }

                        //左右
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3){
                            if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY() &&this.getY() <= enemyTank.getY() +40){
                                return true;
                            }
                            if (this.getX()  >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() + 40 >= enemyTank.getY() &&this.getY() + 40 <= enemyTank.getY() +40){
                                return true;
                            }
                        }
                    }
                }
                break;
        }
        return false;
    }

    //判断坦克是否撞墙
    public boolean isOverWall() {
        switch (this.getDirect()) {
            case 0: //上
                for (int i = 0; i < walls.size(); i++) {
                    Wall wall = walls.get(i);
                    if (((this.getY() >= wall.y) && (this.getY() <= (wall.y + 20))) &&
                            (((this.getX() > wall.x) && (this.getX() < (wall.x + 20)))
                                    || (((this.getX() + 40) > wall.x) && ((this.getX() + 40) < (wall.x + 20))))) {
                        return true;

                    }
                }
                break;
            case 1: //右
                for (int i = 0; i < walls.size(); i++) {
                    Wall wall = walls.get(i);
                    if ((((this.getX() + 60) >= wall.x) && ((this.getX() + 60) <= (wall.x + 20))) &&
                            (((this.getY() > wall.y) && (this.getY() < (wall.y + 20)))
                                    || (((this.getY() + 40) > wall.y) && ((this.getY() + 40) < (wall.y + 20))))) {
                        return true;

                    }
                }
                break;
            case 2: //下
                for (int i = 0; i < walls.size(); i++) {
                    Wall wall = walls.get(i);
                    if ((((this.getY() + 60) >= wall.y) && ((this.getY() + 60) <= (wall.y + 20))) &&
                            (((this.getX() > wall.x) && (this.getX() < (wall.x + 20)))
                                    || (((this.getX() + 40) > wall.x) && ((this.getX() + 40) < (wall.x + 20))))) {
                        return true;
                    }
                }
                break;
            case 3: //左
                for (int i = 0; i < walls.size(); i++) {
                    Wall wall = walls.get(i);
                    if (((this.getX() >= wall.x) && (this.getX() <= (wall.x + 20))) &&
                            (((this.getY() > wall.y) && (this.getY() < (wall.y + 20)))
                                    || (((this.getY() + 40) > wall.y) && ((this.getY() + 40) < (wall.y + 20))))) {
                        return true;

                    }
                }
                break;
        }
        return false;
    }

    @Override
    public void run() {
        while (true){
            //子弹消亡就再创建
            if (isLive && shots.size() < 1){
                Shot shot = null;
                switch (getDirect()){
                    case 0 :
                         shot = new Shot(getX() + 20 ,getY(),0);
                        break;
                    case 1 :
                         shot = new Shot(getX() + 60 ,getY() + 20,1);
                        break;
                    case 2 :
                         shot = new Shot(getX() + 20 ,getY() + 60,2);
                        break;
                    case 3 :
                         shot = new Shot(getX() + 20 ,getY() + 20,3);
                        break;
                }
                shots.add(shot);
                new Thread(shot).start();
            }

            //坦克实现随机移动
            switch (getDirect()){
                case 0 :
                    for (int i =0;i < 50;i++) {
                        if (getY() > 0 && !isOverlap() && !isOverWall()) {
                            moveUp();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    break;
                case 1 :
                    for (int i =0;i < 50;i++) {
                        if (getX() + 60 <1000 && !isOverlap() && !isOverWall()) {
                           moveRight();
                       }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    break;
                case 2 :
                    for (int i =0;i < 50;i++) {
                        //范围还需修改
                        if (getY() + 60 < 710 && !isOverlap() && !isOverWall()) {
                            moveDown();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    break;
                case 3 :
                    for (int i =0;i < 50;i++) {
                        if (getX() > 0  && !isOverlap() && !isOverWall()) {
                            moveLeft();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    break;
            }



            setDirect((int)(Math.random() * 4));
            if (!isLive){
                break;
            }
        }
    }
}
